package com.clw.weblogin.controller;

import com.clw.weblogin.common.entity.ResultEntity;
import com.clw.weblogin.common.utils.JsonUtils;
import com.clw.weblogin.entity.Department;
import com.clw.weblogin.entity.HuanKuan;
import com.clw.weblogin.entity.HuanKuanLiuShui;
import com.clw.weblogin.entity.JieKuan;
import com.clw.weblogin.service.DepartmentService;
import com.clw.weblogin.service.HuanKuanLiuShuiService;
import com.clw.weblogin.service.HuanKuanService;
import com.clw.weblogin.service.JieKuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/huankuan")
public class HuanKuanController {
    @Autowired
    HuanKuanService huanKuanService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    JieKuanService jieKuanService;
    @Autowired
    HuanKuanLiuShuiService huanKuanLiuShuiService;

    @ResponseBody
    @RequestMapping("/select")
    public ResultEntity select(String departmentid, int page, @RequestParam(required = false, defaultValue = "5") int limit,
                               String name, String namenumber, String daikuanmoney, String daikuanbegindate) throws Exception {
        ResultEntity resultEntity = new ResultEntity();
        HuanKuan entity = new HuanKuan();
        entity.setDepartmentid(departmentid);
        if (name != null) {
            if (name != "" && !name.equals("")) {
                entity.setName(name);
            }
        }
        if (namenumber != null) {
            if (namenumber != "" && !namenumber.equals("")) {
                entity.setNamenumber(namenumber);
            }
        }
        if (daikuanmoney != null) {
            if (daikuanmoney != "" && !daikuanmoney.equals("")) {
                entity.setDaikuanmoney(Double.valueOf(daikuanmoney));
            }
        }
        if (daikuanbegindate != null) {
            if (daikuanbegindate != "" && !daikuanbegindate.equals("")) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                entity.setDaikuanbegindate(sdf.parse(daikuanbegindate));
            }
        }
        resultEntity = huanKuanService.select(entity, page, limit);
        return resultEntity;
    }

    @ResponseBody
    @RequestMapping("/insert")
    public ResultEntity insert(String name, String usernumber, String phone, String book, String carnumber, String daikuantype, String daikuanmoney, String daikuanbegindate, String lilv, String yuegong, String jiexidate, String jiebendate, String shoufu, String yue, String beizhu, String danbao, String caozuoren
            , String departmentid, Integer type, String lixi) throws Exception {
        ResultEntity entity = new ResultEntity();
        HuanKuan huankuan = new HuanKuan();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        huankuan.setName(name);
        if (usernumber != "") {
            huankuan.setNamenumber(usernumber);
        }
        if (beizhu != "") {
            huankuan.setBeizhu(beizhu);
        }
        if (book != "") {
            huankuan.setBook(book);
        }
        if (phone != "") {
            huankuan.setPhone(phone);
        }
        if (carnumber != "") {
            huankuan.setCarnumber(carnumber);
        }
        if (daikuantype != "") {
            huankuan.setDaikuantype(daikuantype);
        }
        if (!daikuanmoney.equals("")) {
            huankuan.setDaikuanmoney(Double.valueOf(daikuanmoney));
        }
        if (!daikuanbegindate.equals("")) {
            huankuan.setDaikuanbegindate(sdf.parse(daikuanbegindate));
        }
        if (!lixi.equals("")) {
            huankuan.setLixi(Integer.valueOf(lixi));
        }
//        if (!daikuanenddate.equals("")) {
//            huankuan.setDaikuanenddate(sdf.parse(daikuanenddate));
//        }
        huankuan.setLilv(lilv);

        if (!jiebendate.equals("")) {
            huankuan.setJiebendate(sdf.parse(jiebendate));
        }
        if (!jiexidate.equals("")) {
            huankuan.setJiexidate(sdf.parse(jiexidate));
        }
        if (!shoufu.equals("")) {
            huankuan.setShoufu(Double.valueOf(shoufu));
        }
        if (!yue.equals("")) {
            huankuan.setYue(Double.valueOf(yue));
        }
        if (jiebendate != "") {
            huankuan.setDaikuanenddate(huanKuanService.yujidate(sdf.parse(jiebendate), 30));
        } else if (daikuanbegindate != "") {
            huankuan.setDaikuanenddate(huanKuanService.yujidate(sdf.parse(daikuanbegindate), 30));
        }
        huankuan.setDanbao(danbao);
        huankuan.setCaozuoren(caozuoren);
        huankuan.setDepartmentid(departmentid);
        Department department = new Department();
        department.setDepartmentid(Integer.valueOf(departmentid));
        huankuan.setDepartmentname(departmentService.selectnameByid(department));
        huankuan.setType(type);
        huankuan.setHuankuantype(0);
        if (!yuegong.equals("")) {
            huankuan.setYuegong(Double.valueOf(yuegong));
        } else {
//            //计算月供
//            c1.setTime(sdf.parse(daikuanbegindate));
//            c2.setTime(sdf.parse(daikuanenddate));
//            long days = (c2.getTimeInMillis() - c1.getTimeInMillis()) / (1000 * 3600 * 24);
//
//            if(c2.getTimeInMillis() < c1.getTimeInMillis()){
//                entity.setMessage("错误：放贷时间大于结束时间");
//                return entity;
//            }
//            int year1 = c1.get(Calendar.YEAR);
//            int year2 = c2.get(Calendar.YEAR);
//            int month1 = c1.get(Calendar.MONTH);
//            int month2 = c2.get(Calendar.MONTH);
//            int day1 = c1.get(Calendar.DAY_OF_MONTH);
//            int day2 = c2.get(Calendar.DAY_OF_MONTH);
//            // 获取年的差值 假设 d1 = 2015-8-16  d2 = 2011-9-30
//            int yearInterval = year2 - year1;
//            // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
//            if(month2 < month1 || month2 == month1 && day2 < day1) yearInterval --;
//            // 获取月数差值
//            int monthInterval =  (month2 + 12) - month1  ;
//            if(day2 < day1) monthInterval --;
//            monthInterval %= 12;
//            double temp=huankuan.getDaikuanmoney()/(yearInterval * 12 + monthInterval);
//            int value=(int )(temp*100);
//            temp=value/100;
//            huankuan.setYuegong(temp);
        }
        entity = huanKuanService.insert(huankuan);
        return entity;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public ResultEntity delete(String id, String caozuoren) {
        ResultEntity resultEntity = new ResultEntity();
        HuanKuan huankuan = new HuanKuan();
        huankuan.setId(Integer.valueOf(id));
        List<HuanKuan> list = huanKuanService.selectById(huankuan);
        list.get(0).setType(2);
        resultEntity = huanKuanService.update(list.get(0));
        HuanKuanLiuShui huanKuanLiuShui = new HuanKuanLiuShui();
        huanKuanLiuShui.setHuankuanid(list.get(0).getId());
        huanKuanLiuShui.setName(list.get(0).getName());
        huanKuanLiuShui.setNamenumber(list.get(0).getNamenumber());
        huanKuanLiuShui.setBook(list.get(0).getBook());
        huanKuanLiuShui.setPhone(list.get(0).getPhone());
        huanKuanLiuShui.setCarnumber(list.get(0).getCarnumber());
        huanKuanLiuShui.setDaikuantype(list.get(0).getDaikuantype());
        huanKuanLiuShui.setDaikuanmoney(list.get(0).getDaikuanmoney());
        huanKuanLiuShui.setDaikuanbegindate(list.get(0).getDaikuanbegindate());
        huanKuanLiuShui.setDaikuanenddate(list.get(0).getDaikuanenddate());
        huanKuanLiuShui.setLilv(list.get(0).getLilv());
        huanKuanLiuShui.setYuegong(list.get(0).getYuegong());
        huanKuanLiuShui.setJiexidate(list.get(0).getJiexidate());
        huanKuanLiuShui.setJiebendate(list.get(0).getJiebendate());
        huanKuanLiuShui.setShoufu(list.get(0).getShoufu());
        huanKuanLiuShui.setYue(list.get(0).getYue());
        huanKuanLiuShui.setBeizhu(list.get(0).getBeizhu());
        huanKuanLiuShui.setDaobao(list.get(0).getDanbao());
        huanKuanLiuShui.setHuankuantype("隐藏");
        huanKuanLiuShui.setType(4);
        huanKuanLiuShui.setCaozuoren(caozuoren);
        huanKuanLiuShui.setCaozuoleixing("隐藏");
        huanKuanLiuShui.setDepartmentname(list.get(0).getDepartmentname());
        huanKuanLiuShui.setDepartmentid(list.get(0).getDepartmentid());
        huanKuanLiuShui.setLixi(list.get(0).getLixi());
        huanKuanLiuShui.setCaozuodate(new Date());
        huanKuanLiuShui.setHuanbenji(0.0);
        huanKuanLiuShui.setHuanlixi(0);
        huanKuanLiuShui.setQishu("");
        int i = huanKuanLiuShuiService.insert(huanKuanLiuShui);
        if (i == 1) {
            return resultEntity;
        } else {
            resultEntity.setData(100);
            resultEntity.setMessage("删除方法报错");
            return resultEntity;
        }
    }

    @ResponseBody
    @RequestMapping("/jiesuan")
    public ResultEntity jiesuan(String id) {
        ResultEntity entity = new ResultEntity();
        HuanKuan huanKuan = new HuanKuan();
        huanKuan.setId(Integer.valueOf(id));
        huanKuan.setType(1);
        entity = huanKuanService.update(huanKuan);
        return entity;
    }

    @ResponseBody
    @RequestMapping("/update")
    public ResultEntity update(String id, String name, String namenumber, String phone, String book, String carnumber, String daikuantype, String daikuanmoney, String daikuanbegindate,
                               String daikuanenddate, String lilv, String yuegong, String jiexidate, String jiebendate, String shoufu, String yue, String beizhu, String danbao, String caozuoren
            , String departmentid, Integer type, String lixi) throws Exception {
        ResultEntity resultEntity = new ResultEntity();
      //  HuanKuan huankuan = new HuanKuan();
        HuanKuan selectid=new HuanKuan();
        selectid.setId(Integer.valueOf(id));
        List<HuanKuan> list= huanKuanService.selectById(selectid);
        HuanKuan huankuan=list.get(0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        huankuan.setId(Integer.valueOf(id));
        huankuan.setName(name);
        huankuan.setNamenumber(namenumber);
        huankuan.setBeizhu(beizhu);
        huankuan.setBook(book);
        huankuan.setPhone(phone);
        huankuan.setCarnumber(carnumber);
        huankuan.setDaikuantype(daikuantype);
        if (!lixi.equals("")) {
            huankuan.setLixi(Integer.valueOf(lixi));
        }
        if (!daikuanmoney.equals("")) {
            huankuan.setDaikuanmoney(Double.valueOf(daikuanmoney));
        }
        if (!daikuanbegindate.equals("")) {
            huankuan.setDaikuanbegindate(sdf.parse(daikuanbegindate));
        }
        if (!daikuanenddate.equals("")) {
            huankuan.setDaikuanenddate(sdf.parse(daikuanenddate));
        }
        huankuan.setLilv(lilv);

        if (!jiebendate.equals("")) {
            huankuan.setJiebendate(sdf.parse(jiebendate));
        }
        if (!jiexidate.equals("")) {
            huankuan.setJiexidate(sdf.parse(jiexidate));
        }
        if (!shoufu.equals("")) {
            huankuan.setShoufu(Double.valueOf(shoufu));
        }
        if (!yue.equals("")) {
            huankuan.setYue(Double.valueOf(yue));
        }
        huankuan.setDanbao(danbao);
        huankuan.setDepartmentid(departmentid);
        Department department = new Department();
        department.setDepartmentid(Integer.valueOf(departmentid));
        huankuan.setDepartmentname(departmentService.selectnameByid(department));
        huankuan.setType(type);
        huankuan.setHuankuantype(0);
        if (!yuegong.equals("")) {
            huankuan.setYuegong(Double.valueOf(yuegong));
        } else {
//            //计算月供
//            c1.setTime(sdf.parse(daikuanbegindate));
//            c2.setTime(sdf.parse(daikuanenddate));
//            long days = (c2.getTimeInMillis() - c1.getTimeInMillis()) / (1000 * 3600 * 24);
//
//            if(c2.getTimeInMillis() < c1.getTimeInMillis()){
//                entity.setMessage("错误：放贷时间大于结束时间");
//                return entity;
//            }
//            int year1 = c1.get(Calendar.YEAR);
//            int year2 = c2.get(Calendar.YEAR);
//            int month1 = c1.get(Calendar.MONTH);
//            int month2 = c2.get(Calendar.MONTH);
//            int day1 = c1.get(Calendar.DAY_OF_MONTH);
//            int day2 = c2.get(Calendar.DAY_OF_MONTH);
//            // 获取年的差值 假设 d1 = 2015-8-16  d2 = 2011-9-30
//            int yearInterval = year2 - year1;
//            // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
//            if(month2 < month1 || month2 == month1 && day2 < day1) yearInterval --;
//            // 获取月数差值
//            int monthInterval =  (month2 + 12) - month1  ;
//            if(day2 < day1) monthInterval --;
//            monthInterval %= 12;
//            double temp=huankuan.getDaikuanmoney()/(yearInterval * 12 + monthInterval);
//            int value=(int )(temp*100);
//            temp=value/100;
//            huankuan.setYuegong(temp);
        }
        resultEntity = huanKuanService.update(huankuan);
        HuanKuanLiuShui huanKuanLiuShui = new HuanKuanLiuShui();
        huanKuanLiuShui.setHuankuanid(Integer.valueOf(id));
        huanKuanLiuShui.setName(name);
        huanKuanLiuShui.setNamenumber(namenumber);
        huanKuanLiuShui.setBeizhu(beizhu);
        huanKuanLiuShui.setBook(book);
        huanKuanLiuShui.setPhone(phone);
        huanKuanLiuShui.setCarnumber(carnumber);
        huanKuanLiuShui.setDaikuantype(daikuantype);
        if (!lixi.equals("")) {
            huanKuanLiuShui.setLixi(Integer.valueOf(lixi));
        }
        if (!daikuanmoney.equals("")) {
            huanKuanLiuShui.setDaikuanmoney(Double.valueOf(daikuanmoney));
        }
        if (!daikuanbegindate.equals("")) {
            huanKuanLiuShui.setDaikuanbegindate(sdf.parse(daikuanbegindate));
        }
        if (!daikuanenddate.equals("")) {
            huanKuanLiuShui.setDaikuanenddate(sdf.parse(daikuanenddate));
        }
        huanKuanLiuShui.setLilv(lilv);

        if (!jiebendate.equals("")) {
            huanKuanLiuShui.setJiebendate(sdf.parse(jiebendate));
        }
        if (!jiexidate.equals("")) {
            huanKuanLiuShui.setJiexidate(sdf.parse(jiexidate));
        }
        if (!shoufu.equals("")) {
            huanKuanLiuShui.setShoufu(Double.valueOf(shoufu));
        }
        if (!yue.equals("")) {
            huanKuanLiuShui.setYue(Double.valueOf(yue));
        }
        huanKuanLiuShui.setDaobao(danbao);
        huanKuanLiuShui.setCaozuoren(caozuoren);
        huanKuanLiuShui.setDepartmentid(departmentid);
        huanKuanLiuShui.setDepartmentname(departmentService.selectnameByid(department));
        huanKuanLiuShui.setType(0);
        huanKuanLiuShui.setHuankuantype("修改");
        huanKuanLiuShui.setYuegong(Double.valueOf(yuegong));
        huanKuanLiuShui.setCaozuoleixing("修改");
        huanKuanLiuShui.setCaozuodate(new Date());
        huanKuanLiuShui.setHuanlixi(0);
        huanKuanLiuShui.setHuanbenji(0.0);
        huanKuanLiuShui.setQishu("");
        int i = huanKuanLiuShuiService.insert(huanKuanLiuShui);
        if (i == 1) {
            return resultEntity;
        } else {
            resultEntity.setData(100);
            resultEntity.setMessage("修改功能报错");
            return resultEntity;
        }

    }

    @ResponseBody
    @RequestMapping("/selectDepartment")
    public List<Department> selectDepartment() {
        ResultEntity resultEntity = new ResultEntity();
        List<Department> list = departmentService.selectdepart();
        resultEntity.setMessage("搜索成功");
        resultEntity.setState(200);
        resultEntity.setData(list);
        return list;
    }

    @ResponseBody
    @RequestMapping("/selectBydate")
    public ResultEntity selectBydate(String departmentid, String name, String namenumber, String daikuanmoney, String daikuanbegindate, int page, @RequestParam(required = false, defaultValue = "5") int limit) throws Exception {
        ResultEntity resultEntity=new ResultEntity();
        HuanKuan huanKuan = new HuanKuan();
        //据上次结算周期前5天
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, +5);
        Date date = calendar.getTime();
        String d = sdf.format(date);
        huanKuan.setDaikuanenddate(sdf.parse(d));
        if (name != null) {
            if (name != "" && !name.equals("")) {
                huanKuan.setName(name);
            }
        }
        if (namenumber != null) {
            if (namenumber != "" && !namenumber.equals("")) {
                huanKuan.setNamenumber(namenumber);
            }
        }
        if (daikuanmoney != null) {
            if (daikuanmoney != "" && !daikuanmoney.equals("")) {
                huanKuan.setDaikuanmoney(Double.valueOf(daikuanmoney));
            }
        }
        if (daikuanbegindate != null) {
            if (daikuanbegindate != "" && !daikuanbegindate.equals("")) {
                huanKuan.setDaikuanbegindate(sdf.parse(daikuanbegindate));
            }
        }
        if(Integer.valueOf(departmentid)!=1){
            huanKuan.setDepartmentid(departmentid);
        }

        huanKuan.setType(0);
        resultEntity = huanKuanService.selectByDate(huanKuan, page, limit);
        return resultEntity;
    }

}
